
dofile(arg[1] or "Secretary.lua")

local function totime(value)
	if value > 3600 then
		local h, m, s
		h = math.floor(value / 3600)
		value = value - h * 3600
		m = math.floor(value / 60)
		s = math.floor(value - m * 60 + 0.5)
		return string.format("%dh %dm %ds", h, m, s)
	elseif value > 60 then
		local m, s
		m = math.floor(value / 60)
		s = math.floor(value - m * 60 + 0.5)
		return string.format("%dm %ds", m, s)
	else
		return string.format("%ds", math.floor(value + 0.5))
	end
end

local function get_date(profile, raid_id, time)
	local r = profile.raids[raid_id]
	local start_date = r.start_date
	local start_time = r.start_time
	local d = {}
	d.month, d.day, d.year, d.hour, d.min, d.sec = start_date:match("(%d+)/(%d+)/(%d+) (%d+):(%d+):(%d+)")
	return os.date("%H:%M:%S", os.time(d) - start_time + time)
end

local function get_player(profile, name)
	return string.format("<a href=\"http://armory.wow-europe.com/character-sheet.xml?r=%s&n=%s\" title=\"%s\" class=\"player-%s\">%s</a>", 
		profile.config.realm, name, profile.chars[name].comment, profile.chars[name].class:lower(), name)
end

local function get_attendance(profile, out, start, _end)
	local players = {}
	for _, event in ipairs(profile.events) do
		if event.raid_id >= start and event.raid_id <= _end then
			if event.type == "enter" then
				local state = players[event.player]
				if not state then
					state = { active = 0, afk = 0, offline = 0, start = get_date(profile, event.raid_id, event.time) }
					players[event.player] = state
				elseif state.state ~= "left" and state.last_time then
					local delta_time = event.time - state.last_time
					state[state.state] = state[state.state] + delta_time
				end
				state.last_time = event.time
				state.state = "active"
			elseif event.type == "leave" then
				local state = players[event.player]
				assert(state)
				if state.state ~= "left" then
					local delta_time = event.time - state.last_time
					state.last_time = event.time
					state[state.state] = state[state.state] + delta_time
					state.state = "left"
					state._end = get_date(profile, event.raid_id, event.time)
				end
			elseif event.type == "afk" or event.type == "offline" then
				local state = players[event.player]
				assert(state)
				local delta_time = event.time - state.last_time
				state.last_time = event.time
				assert(state.state == "active")
				state[state.state] = state[state.state] + delta_time
				state.state = event.type
			elseif event.type == "return" then
				local state = players[event.player]
				assert(state)
				local delta_time = event.time - state.last_time
				state.last_time = event.time
				assert(state.state ~= "active" and state.state ~= "left")
				state[state.state] = state[state.state] + delta_time
				state.state = "active"
			end
		end
	end
	local sorted_players = {}
	for name in pairs(players) do
		sorted_players[#sorted_players + 1] = name
	end
	table.sort(sorted_players, function (a, b)
		local ca, cb = profile.chars[a].class, profile.chars[b].class
		if ca ~= cb then
			return ca < cb
		else
			return a < b
		end
	end)
	out:write("<h3>Attendance</h3>\n")
	out:write("<table>\n")
	out:write("<tr><th>Name</th><th>Date Entered</th><th>Date Left</th><th>Time Active</th><th>Time AFK</th><th>Time Offline</th></tr>\n")
	for _, name in ipairs(sorted_players) do
		local info = players[name]
		out:write(string.format("<tr><td class=\"player-name\">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
			get_player(profile, name), info.start, info._end,
			totime(info.active), totime(info.afk), totime(info.offline)))
	end
	out:write("</table>\n")
end

local function build_item_link(itemlink)
	local color, id, name = itemlink:match("|cff([0-9a-f]+)|Hitem:(%d+):[^[]*(%[[^%]]+%])")
	return string.format([[<a style="color:#%s;" class="itemlink" href="http://armory.wow-europe.com/item-info.xml?i=%s">%s</a>]], color, id, name)
end

local function get_loots(profile, out, start, _end)
	out:write("<h3>Loots</h3>\n")
	out:write("<table>\n")
	out:write("<tr><th>Time</th><th>Player</th><th>Amount</th><th>Itemlink</th></tr>\n")
	for _, event in ipairs(profile.events) do
		if event.raid_id >= start and event.raid_id <= _end then
			if event.type == "loot" then
				out:write(string.format("<tr><td>%s</td><td class=\"player-name\">%s</td><td>%s</td><td>%s</td></tr>\n", get_date(profile, event.raid_id, event.time), get_player(profile, event.name), event.count, build_item_link(event.item)))
			end
		end
	end
	out:write("</table>\n")
end	

local function get_kills(profile, out, start, _end)
	out:write("<h3>Kills</h3>\n")
	out:write("<table>\n")
	out:write("<tr><th>Time</th><th>Boss</th></tr>\n")
	for _, event in ipairs(profile.events) do
		if event.raid_id >= start and event.raid_id <= _end then
			if event.type == "kill" then
				out:write(string.format("<tr><td>%s</td><td>%s</td></tr>\n", get_date(profile, event.raid_id, event.time), event.name))
			end
		end
	end
	out:write("</table>\n")
end	

local function get_wipes(profile, out, start, _end)
	out:write("<h3>Wipes</h3>\n")
	out:write("<table>\n")
	out:write("<tr><th>Time</th></tr>\n")
	for _, event in ipairs(profile.events) do
		if event.raid_id >= start and event.raid_id <= _end then
			if event.type == "wipe" then
				out:write(string.format("<tr><td>%s</td></tr>\n", get_date(profile, event.raid_id, event.time)))
			end
		end
	end
	out:write("</table>\n")
end	

local function get_raid_info(profile, start, _end)
	local zones = {}
	local i, j = start
	local start_date, end_date, start_time
	while i <= _end do
		local r = profile.raids[i]
		if not start_date then
			start_date = r.start_date
			start_time = r.start_time
			j = i
		end
		if r.zone and r.zone ~= "" then
			zones[r.zone] = true
		end
		end_date = r.end_date
		if end_date then
			local zone, first = "", true
			for z in pairs(zones) do
				if not first then
					zone = zone .. ", " .. z
				else
					zone, first = z
				end
			end
			return j, i, zone, start_date, end_date, start_time
		end
		i = i + 1
	end
end

output = io.open("report.html", "w")
output:write([[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
<title>Attendance</title>
<style type="text/css">
body { background-color:#C0C0C0; }
table { border:outset 1px; }
td, th { border:inset 1px; }
th { background-color:#808080; }
td { background-color:#A0A0A0; }
a { font-weight:bold; }
.player-name { background-color:#606060; }
.player-druid { color:#ff7c0a; }
.player-hunter { color:#aad372; }
.player-mage { color:#68ccef; }
.player-paladin { color:#f48cba; }
.player-priest { color:#ffffff; }
.player-rogue { color:#fff468; }
.player-shaman { color:#00dbba; }
.player-warlock { color:#9382c9; }
.player-warrior { color:#c69b6d; }

</style>
</head>
<body>
<h1>Secretary offline report</h1>
]])

local profile = SecretaryDB.profiles.Default

local start, _end = 1, #profile.raids

while start ~= _end do
	local final, zone, start_date, end_date, start_time
	start, final, zone, start_date, end_date, start_time = get_raid_info(profile, start, _end)
	if not final then break end
	output:write(string.format("<h2>Raid #%d-%d</h2>\n", start, final))
	output:write("<table>\n")
	output:write(string.format("<tr><th>Start Date</th><td>%s</td></tr>\n", start_date))
	output:write(string.format("<tr><th>End Date</th><td>%s</td></tr>\n", end_date))
	output:write(string.format("<tr><th>Zone</th><td>%s</td></tr>\n", zone))
	output:write("</table>\n")
	get_attendance(profile, output, start, final)
	-- get_events(profile.events)
	get_wipes(profile, output, start, final)
	get_loots(profile, output, start, final)
	get_kills(profile, output, start, final)
	
	start = final + 1
end
output:write("</body></html>\n")
output:close()
